package controller

import (
	"slapp3/backend/application/domain"
)

type ConnectController struct {
	connectRepo ConnectRepository
	sslinkRepo  SslinkRepository
}

// NewConnectController creates a new ConnectController instance
func NewConnectController(repo ConnectRepository, sslinkRepo SslinkRepository) *ConnectController {
	return &ConnectController{
		connectRepo: repo,
		sslinkRepo:  sslinkRepo,
	}
}

func (c *ConnectController) CreateConnection(profile domain.Profile) error {
	if err := c.connectRepo.CreateConnection(profile); err != nil {
		return err
	}
	return nil
}

func (c *ConnectController) GetConnection(id uint) (domain.Profile, error) {
	profile, err := c.connectRepo.GetConnection(id)
	if err != nil {
		return domain.Profile{}, err
	}
	return profile, nil
}

func (c *ConnectController) UpdateConnection(profile domain.Profile) error {
	if err := c.connectRepo.UpdateConnection(profile); err != nil {
		return err
	}
	return nil
}

func (c *ConnectController) DeleteConnection(id uint) error {
	if err := c.connectRepo.DeleteConnection(id); err != nil {
		return err
	}
	return nil
}
func (c *ConnectController) ListConnections(page, pageSize int) ([]domain.Profile, int64, error) {
	profiles, total, err := c.connectRepo.ListConnections(page, pageSize)
	if err != nil {
		return nil, 0, err
	}
	return profiles, total, nil
}

func (c *ConnectController) Connect(id uint) error {
	profile, err := c.GetConnection(id)
	if err != nil {
		return err
	}
	c.sslinkRepo.SetProfile(&profile)

	if _, err := c.sslinkRepo.InitAuth(); err != nil {
		return err
	}
	if err := c.sslinkRepo.PasswordAuth(); err != nil {
		return err
	}
	if err := c.sslinkRepo.SetupTunnel(); err != nil {
		return err
	}
	return nil
}

func (c *ConnectController) Disconnect() error {
	// Implement disconnection logic here
	return nil
}
